package inet.app.tuvi.util;

import inet.app.tuvi.models.DeviceInfoModel;

import java.lang.reflect.Method;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

public final class DeviceUtil {

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public static boolean isTablet(Context context) {
		boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
		boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
		return (xlarge || large);
	}

	@SuppressWarnings("deprecation")
	public static DeviceInfoModel getDeviceInfo(Activity activity) {
		/* First, get the Display from the WindowManager */
		Display display = ((WindowManager) activity
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

		/* Now we can retrieve all display-related infos */
		int width = display.getWidth();
		int height = display.getHeight();
		int orientation = getOrientation(activity);
		DeviceInfoModel deviceInfo = new DeviceInfoModel();
		deviceInfo.width = width;
		deviceInfo.height = height;
		deviceInfo.orientation = orientation;
		deviceInfo.isTablet = isTablet(activity);

		return deviceInfo;
	}

	/**
	 * get orientation of activity
	 * 
	 * @param activity
	 * @return ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT
	 */
	public static int getOrientation(Activity activity) {
		Configuration config = activity.getResources().getConfiguration();
		return config.orientation;
	}

	/**
	 * 
	 * @return serial number device
	 */
	public static String getSerialNumber(Context context) {
		try {
			TelephonyManager telephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = telephonyManager.getDeviceId();
			return imei;
		} catch (Exception e) {
			String serialNum;
			try {
				Class<?> classLoader = Class
						.forName("android.os.SystemProperties");
				Method method = classLoader.getMethod("get", String.class,
						String.class);
				serialNum = (String) (method.invoke(classLoader, "ro.serialno"));
			} catch (Exception exception) {
				serialNum = "W80082PE8YA";
			}
			return serialNum;
		}
	}

	/**
	 * The name of the industrial design.
	 * 
	 * @return
	 */
	public static String getDeviceName() {
		return android.os.Build.DEVICE;
	}

	/**
	 * get model & product of device
	 * 
	 * @return
	 */
	public static String getModelAndProduct() {
		return android.os.Build.MODEL + " (" + android.os.Build.PRODUCT + ")";
	}

	/**
	 * Get device ID of each device
	 * 
	 * @param context
	 * @return number device id
	 */
	public static String getDeviceId(Context context) {
		return Settings.Secure.getString(context.getContentResolver(),
				Settings.Secure.ANDROID_ID);
	}
}
